/**
 * New node file
 */

var API = require('wechat-enterprise').API;

module.exports = (function(){
	
	//Wechat Api
	var wxapi = null;
	
	/**
	 * Handle weixin errors.
	 */
	function handleWxError(err, cb) {

		if (err) {
			sails.log.error(err);
		}
		return cb(new Error(err));

	};
	
	function getOneFromArray(array,id){
		if(array == null || !array.length){
			return null;
		}
		for (var i = 0; i < array.length; i++) {
			if(array[i].id == id){
				return array[i];
			}
		}
		return null;
	}
	
	var wechat = {
		
		init : function (config){
			
			wxapi = new API(config.corpid, config.secret, config.agentid);
			
			if(sails.cache == undefined){
				sails.cache = {};
			}
			sails.cache.users = null;
			sails.cache.depts = null;
			
		},
		
		findUser : function(connection, collection, options, cb){
			var where = options.where;
			sails.log.info('Call weixin api : [ findUser ]');
			
			try{
				if(sails.cache.users == null){
					wxapi.getDepartmentUsersDetail(1, 1, 0, function(err, result) {

						if (!err && result.errcode == 0) {
							sails.log.info('Call weixin api success : [ findUser ]');
							sails.cache.users = result.userlist;
							return cb(null, sails.cache.users);
						} else {
							return handleWxError(err, cb);
						}

					});
				}else{
					return cb(null, sails.cache.users);
				}
			}catch(e){
				return cb(e);
			}
		},
		
		findOneUser : function(connection, collection, options, cb){
			var where = options.where;
			sails.log.info('Call weixin api : [ findOneUser ]');
			try{
				function _userhandler(){
					var user = getOneFromArray(sails.cache.users, where.userid);
					if(user == null){
						return cb(new Error('Can not find user [id:'+ where.userid +']'));
					}else{
						return cb(null, user);
					}
				}
				
				if(sails.cache.users == null){
					wechat.findUser(connection, collection, options, function(err, result){
						return _userhandler();
					})
				}else{
					return _userhandler();
				}
				
			}catch(e){
				return cb(e);
			}
		},
		
		findDepartment : function(connection, collection, options, cb){
			sails.log.info('Call weixin api : [ findDepartment ]');
			
			try{
				if(sails.cache.depts == null){
					wxapi.getDepartments(function(err, result) {
						if (!err && result.errcode == 0) {
							sails.log.info('Call weixin api success : [ findDepartment ]');
							sails.cache.depts = result.department;
							return cb(null, sails.cache.depts);
						} else {
							return handleWxError(err, cb);
						}
					});
				}else{
					return cb(null, sails.cache.depts);
				}
			}catch(e){
				return cb(e);
			}
			
		},
		
		findOneDepartment : function(connection, collection, options, cb){
			sails.log.info('Call weixin api : [ findOneDepartment ]');
			
			try{
				
				function _depthandler(){
					var dept = getOneFromArray(sails.cache.depts, where.id);
					if(dept == null){
						return cb(new Error('Can not find department [id:'+ where.id +']'));
					}else{
						wxapi.getDepartmentUsersDetail(dept.id, 0, 0, function(err, details) {
							if(details.userlist){
								dept.userlist = details.userlist;
							}
							return cb(null, dept); 
						})
					}
				}
				
				if(sails.cache.depts == null){
					wechat.findDepartment(connection, collection, options, function(err, result){
						return _depthandler();
					})
				}else{
					return _depthandler();
				}
			}catch(e){
				return cb(e);
			}
			
		}
	}
	
	return wechat;
	
})();
